The following example demonstrates how to provide, through a style, a new ControlTemplate for DataRow objects. The columns that are contained in the grid will be limited to those specified in the ItemProperties of the DataGridCollectionViewSource.
This example assumes that the grid is bound to the Employees table of the Northwind database.
XAML |
Copy Code |
---|---|
<Grid> <Grid.Resources> <xcdg:DataGridCollectionViewSource x:Key="cvs_employees" Source="{Binding Source={x:Static Application.Current}, Path=Employees}" AutoCreateItemProperties="False"> <xcdg:DataGridCollectionViewSource.ItemProperties> <xcdg:DataGridItemProperty Name="LastName"/> <xcdg:DataGridItemProperty Name="FirstName"/> <xcdg:DataGridItemProperty Name="Photo"/> <xcdg:DataGridItemProperty Name="Title"/> <xcdg:DataGridItemProperty Name="Notes"/> <xcdg:DataGridItemProperty Name="EmployeeID"/> <xcdg:DataGridItemProperty Name="TitleOfCourtesy"/> <xcdg:DataGridItemProperty Name="HireDate"/> <xcdg:DataGridItemProperty Name="Extension"/> </xcdg:DataGridCollectionViewSource.ItemProperties> </xcdg:DataGridCollectionViewSource> <ControlTemplate x:Key="titleLessCell" TargetType="xcdg:DataCell"> <ContentPresenter Content="{xcdg:CellContentBinding}" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"/> </ControlTemplate> <Style x:Key="customCardViewDataRow" TargetType="{x:Type xcdg:DataRow}"> <Setter Property="Background" Value="Transparent"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type xcdg:DataRow}"> <Border x:Name="PART_RowFocusRoot" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"> <Grid> <StackPanel> <DockPanel> <!-- The photo is at the left. --> <Grid DockPanel.Dock="Left" Margin="3,4,2,2" MaxWidth="85" MaxHeight="85"> <xcdg:DataCell FieldName="Photo" Template="{StaticResource titleLessCell}"/> </Grid> <StackPanel Margin="8,0,0,0"> <StackPanel Orientation="Horizontal"> <xcdg:DataCell FieldName="TitleOfCourtesy" FontSize="16" Template="{StaticResource titleLessCell}"/> <TextBlock Text=" "/> <xcdg:DataCell FieldName="FirstName" FontSize="16" Template="{StaticResource titleLessCell}"/> <TextBlock Text=" "/> <xcdg:DataCell FieldName="LastName" FontSize="16" Template="{StaticResource titleLessCell}"/> </StackPanel> <xcdg:DataCell FieldName="Title" FontSize="14" Template="{StaticResource titleLessCell}"/> <Border BorderThickness="1" BorderBrush="#999999" Margin="0,2,0,2"/> <StackPanel x:Name="PART_CellsHost" Orientation="Vertical" Grid.IsSharedSizeScope="True"/> </StackPanel> </DockPanel> <Expander Header="Notes" Padding="5" TextElement.Foreground="{TemplateBinding Foreground}"> <xcdg:DataCell FieldName="Notes" Template="{StaticResource titleLessCell}"/> </Expander> </StackPanel> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> <Setter Property="FocusVisualStyle" Value="{x:Null}"/> </Style> </Grid.Resources> <xcdg:DataGridControl x:Name="OrdersGrid" ItemsSource="{Binding Source={StaticResource cvs_employees}}" ItemContainerStyle="{StaticResource customCardViewDataRow}" View="CardView"> <xcdg:DataGridControl.Columns> <xcdg:Column FieldName="Notes" TextWrapping="Wrap"/> </xcdg:DataGridControl.Columns> </xcdg:DataGridControl> </Grid> |